package net.dass.deadpigeon.server.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import net.dass.deadpigeon.server.DeadPigeon;

public class DeadPigeonDAO {
	
	public List<DeadPigeon> list() {
		final EntityManager em = EMFService.get().createEntityManager();
		final Query q = em.createQuery("select m from Todo m");

		return (q.getResultList());
	}

	public void add(String userId, DeadPigeon pigeon) {
		synchronized (this) {
			final EntityManager em = EMFService.get().createEntityManager();
			
			em.persist(pigeon);
			em.close();
		}
	}
	
//	public List<DeadPigeon> findBy(final Map<String, String> properties) {
//		final EntityManager em = EMFService.get().createEntityManager();
//		final Query q = em.createQuery("select t from DeadPigeon t where t.email = :email");
//		
//		q.setParameter("email", email);
//		return (q.getResultList());
//	}

	public List<DeadPigeon> findBy(final String email) {
		final EntityManager em = EMFService.get().createEntityManager();
		final Query q = em.createQuery("select t from DeadPigeon t where t.email = :email");
		
		q.setParameter("email", email);
		return (q.getResultList());
	}

	public void remove(final long id) {
		final EntityManager em = EMFService.get().createEntityManager();
		
		try {
			em.remove(em.find(DeadPigeon.class, id));
		} finally {
			em.close();
		}
	}
}
